package com.tongqu.task;

import com.tongqu.entity.ForwardData;
import com.tongqu.entity.GameJump;
import com.tongqu.service.JumpDataService;
import com.tongqu.service.TableService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


import java.text.SimpleDateFormat;
import java.util.*;

import static io.netty.buffer.Unpooled.copiedBuffer;

@Component
public class ScheduleTask {

    private Logger logger = LoggerFactory.getLogger(ScheduleTask.class);
    static final String click = "click";
    static final String visit = "visit";
    @Autowired
    private TableService tableService;
    @Autowired
    private JumpDataService jumpDataService;
    @Autowired
    private RedisTemplate redisTemplate;


//    @Scheduled(cron = "0 0 0 * * ?")
//    @Scheduled(cron = "*/5 * * * * ?")
    public void EveryDayTask() {
//        logger.info("定时任务开始执行");
//        dairyTask();
//        logger.info("定时任务执行完毕");
    }

//    @Scheduled(cron = "0 0 0 1 1-12 ?")
    public void MonthWork(){
        if(jumpDataService.clearAllData()){
            logger.info("清除所有数据成功");
        }else {
            logger.info("清除所有数据失败");
        }

    }
    private void dairyTask() {

        saveIntoMySql();
       // clearRedisData();

    }


    public void weekTask() {
        logger.info("开始清除表");
        List<String> tables = tableService.findAllTables();
        if (tables == null || tables.size() <= 0) {
            logger.info("数据库为空");
            return;
        } else {
            for (String table :
                    tables) {
                Date tableTime = tableService.getCreateTableTime(table);
                Date now = new Date();

                tableService.dropTable(table);
                logger.info(table + "已经被清除" + now);

            }
        }
    }


    private void clearRedisData() {
        logger.info("开始清除redis缓存");
        String clickPrex = click + "*";
        String exposurePrex = visit + "*";
        Set<String> keys = redisTemplate.keys(clickPrex);
        if (keys != null || keys.size() != 0) {
            redisTemplate.delete(keys);
        }
        Set keys1 = redisTemplate.keys(exposurePrex);
        if (keys != null || keys.size() != 0) {
            redisTemplate.delete(keys1);
        }
        long num = keys.size() + keys1.size();
        logger.info("清除数据缓存数据量为" + num);
    }

    private List getRedisData(String str) {

        Set<String> keys = redisTemplate.keys(str + "_*");
        List<GameJump> list = new ArrayList<>();

        for (String s :
                keys) {
            String[] strings = s.split("_");
            String type = strings[0];
            String sourceAppId = strings[1];
            String targetAppId = strings[2];
            Long count = Long.parseLong((String) redisTemplate.opsForValue().get(s));

            GameJump gameJump = new GameJump();
            gameJump.setSourceAppId(sourceAppId);
            gameJump.setTargetAppId(targetAppId);
            gameJump.setCount(count);
            gameJump.setType(type);
            gameJump.setTime(new Date());
            list.add(gameJump);
        }
        return list;
    }


    public void saveIntoMySql() {

        List clickList = getRedisData(click);
        List visitList = getRedisData(visit);
        logger.info("开始数据存储");
        boolean clickFlag = jumpDataService.saveData(clickList);
        boolean exposureFlag = jumpDataService.saveData(visitList);
        if (clickFlag && exposureFlag) {
            logger.info("数据存储成功");
            boolean f = jumpDataService.saveDataIntoTempData();

        } else {
            logger.info("数据存储失败");
        }

    }

    public static void main(String[] args) {
        System.out.println(12 == 0xc ?(0xc != 12?10:11):1);
    }
}
